<template>
  <div class="roles-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card @close="addUserVisible = false">
      <el-button type="primary" @click="openAddUserDialog">添加角色</el-button>
      <!-- 添加角色对话框 -->
      <AddUserDialog @updateList="updateList"></AddUserDialog>

      <el-table border stripe width="100%" :data="rolesList">
        <!-- 展开列 -->
        <el-table-column type="expand">
          <!-- 一级权限和二级三级权限模块 -->
          <template slot-scope="scope">
            <RightsDetails :scope="scope"></RightsDetails>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="用户名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="getId(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              @click="setRights(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改角色对话框 -->
      <EditDialog :id="clickedId" @updateList="updateList"></EditDialog>

      <!-- 分配权限对话框 -->
      <SettingRightDialog
        :row="settingRightsData"
        @updateList="updateList"
      ></SettingRightDialog>
    </el-card>
  </div>
</template>

<script>
import AddUserDialog from '@/components/roles/AddUserDialog.vue'
import EditDialog from '@/components/roles/EditUserDialog.vue'
import SettingRightDialog from '@/components/roles/SettingRightDialog.vue'
import RightsDetails from '@/components/roles/RightsDetails.vue'
export default {
  name: 'Roles',
  data() {
    return {
      rolesList: [],
      editUserVisible: false,
      setRightsVisible: false,
      roleUserData: {
        roleName: '',
        roleDesc: ''
      },
      roleFormRules: {
        roleName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入用户描述', trigger: 'blur' }
        ]
      },
      editUserData: {},
      clickedId: 0,
      settingRightsData: {}
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error('请求数据失败！')

      this.$message.success('请求数据成功！')
      this.rolesList = res.data
    },
    async getId(id) {
      this.clickedId = id
      this.$store.commit('roles/editUserVisibleState', true)
    },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http
            .delete('roles/' + id)
            .then((res) => {
              this.getRolesList()
            })
            .catch((error) => {
              return error
            })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    setRights(row) {
      this.settingRightsData = row
      this.$store.commit('roles/settingUserVisibleState', true)
    },
    openAddUserDialog() {
      this.$store.commit('roles/addUserVisibleState', true)
    },
    updateList() {
      this.getRolesList()
    }
  },
  components: {
    AddUserDialog,
    EditDialog,
    SettingRightDialog,
    RightsDetails
  }
}
</script>

<style scoped lang="less"></style>
